package com.way.gesture;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.CheckBox;

public class FragmentHelp extends DialogFragment implements
		DialogInterface.OnClickListener {
	private CheckBox mCheckBox;
	private WebView mWebView;

	public void onAttach(Activity paramActivity) {
		super.onAttach(paramActivity);
	}

	public void onClick(DialogInterface dialogInterface, int id) {
		boolean isChecked = mCheckBox.isChecked();
		ContentResolver contentResolver = getDialog().getContext()
				.getContentResolver();
		Settings.System.putInt(contentResolver, "gesture_show_help",
				isChecked ? 0 : 1);
		dismiss();
	}

	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
	}

	public Dialog onCreateDialog(Bundle bundle) {
		View rootView = getActivity().getLayoutInflater().inflate(
				R.layout.fragment_help, null);
		mCheckBox = ((CheckBox) rootView.findViewById(R.id.check_box));
		boolean isCheck = Settings.System.getInt(getActivity()
				.getContentResolver(), "gesture_show_help", 1) == 0 ? true
				: false;
		mCheckBox.setChecked(isCheck);
		rootView.findViewById(R.id.checkLayout).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View view) {
						mCheckBox.setChecked(!mCheckBox.isChecked());
					}
				});
		this.mWebView = ((WebView) rootView.findViewById(R.id.web_view));
		this.mWebView.loadUrl("file:///android_asset/720.gif");
		WebSettings webSettings = this.mWebView.getSettings();
		webSettings.setUseWideViewPort(false);
		webSettings
				.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
		webSettings.setLoadWithOverviewMode(true);
		this.mWebView.setHorizontalScrollBarEnabled(false);
		this.mWebView.setVerticalScrollBarEnabled(false);
		this.mWebView.setBackgroundColor(0);
		this.mWebView.setFocusable(false);
		this.mWebView.setClickable(false);
		this.mWebView.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
				return true;
			}
		});
		return new AlertDialog.Builder(getActivity())
				.setTitle(R.string.getsure_help_menu).setView(rootView)
				.setPositiveButton(android.R.string.ok, this).create();
	}

	public void onDestroy() {
		if (this.mWebView != null) {
			this.mWebView.loadUrl("about:blank");
			this.mWebView.stopLoading();
			this.mWebView = null;
		}
		this.mCheckBox = null;
		super.onDestroy();
	}

	public void onStart() {
		super.onStart();
		getDialog().getWindow().getDecorView().setVisibility(View.INVISIBLE);
		new Handler().postDelayed(new Runnable() {
			public void run() {
				FragmentHelp.this.getDialog().getWindow().getDecorView()
						.setVisibility(View.VISIBLE);
			}
		}, 500L);
	}

	public void show(FragmentManager fragmentManager, Context context,
			boolean notShow) {
		if ((Settings.System.getInt(context.getContentResolver(),
				"gesture_show_help", 1) == 0) && (notShow))
			return;
		super.show(fragmentManager, "help");
	}
}
